home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 11 / FM Towns Free Software Collection 11.iso / t_os / tool / mmlc / source / m2libm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-17  |  4.6 KB  |  279 lines

  1. /* ------------------------------------------------------------------
  2.                         MML compiler [M2]
  3.                programmed by S.Yamamoto (SHINNOSUKE)
  4.                 m2libm.c  --  other function source
  5. ------------------------------------------------------------------ */
  6.  
  7. #include    <stdio.h>
  8. #include    <stdlib.h>
  9. #include    <ctype.h>
  10. #include    <string.h>
  11. #include    <limits.h>
  12. #include    <math.h>
  13. #include    "m2.h"
  14.  
  15. #define    SUCCSESS    0
  16. #define    FAILURE    (-1)
  17. #define    TRUE    1
  18. #define    FALSE    0
  19. #define    ERR    (-1)
  20. #define    YES    1
  21. #define    NO    0
  22.  
  23. char    *expMsg = "EXPRESSION";
  24. char    *expBuf;
  25. int    ch;
  26.  
  27. /* 四則演算処理 */
  28.  
  29. void    readch( void )
  30. {
  31.     ch = *expBuf;
  32.     expBuf++;
  33.     return;
  34. }
  35.  
  36. int    number( void )
  37. {
  38.     int    x;
  39.     int    dummy;
  40.     int    sign;
  41.     int    radix = 10;
  42.  
  43.     if ( ch == '+' || ch == '-' ) {
  44.         sign = ch;
  45.         readch();
  46.     }
  47.     if (! isdigit(ch)) errMsg(MSG_syntax,expMsg);
  48.     if (ch == '0') {
  49.         if(toupper(*expBuf) == 'X')    radix=16;
  50.         if(toupper(*expBuf) == 'B')    radix=2;
  51.         if(toupper(*expBuf) == 'O')    radix=8;
  52.         if(radix != 10)    expBuf++;
  53.     }
  54.     x = ch - '0';
  55.     for(;;) {
  56.         readch();
  57.         ch=toupper(ch);
  58.         if (isdigit(ch) == 0 && ch<'A' && ch>'F')    break;
  59.  
  60.         dummy=(ch>='A'&&ch<='F')?ch-'A'+10:ch-'0';
  61.         if(dummy<0 || dummy>=radix)    break;
  62.         x=dummy+x*radix;
  63.     }
  64.  
  65.     if (sign == '-') return -x;  else return x;
  66. }
  67.  
  68. int    factor( void )
  69. {
  70.     int    x;
  71.  
  72.     if (ch != '(') return number();
  73.     readch();
  74.     x = expression();
  75.     if (ch != ')')
  76.         errMsg(MSG_syntax,expMsg);
  77.     readch();
  78.     return x;
  79. }
  80.  
  81. int    term( void )
  82. {
  83.     int    x;
  84.     int    y;
  85.  
  86.     x = factor();
  87.     for ( ; ; )
  88.         if (ch == '*') {
  89.             readch();  x *= factor();
  90.         } else if (ch == '/') {
  91.             readch();  y = factor();
  92.             if (y == 0) errMsg(MSG_freerr,"Divide by zero(EXPRESSION)");
  93.             x /= y;
  94.         } else if (ch == '%') {
  95.             readch();  x %= factor();
  96.         } else break;
  97.     return x;
  98. }
  99.  
  100. int    expression( void )
  101. {
  102.     int    x;
  103.  
  104.     x = term();
  105.     for ( ; ; )
  106.         if (ch == '+') {
  107.             readch();  x += term();
  108.         } else if (ch == '-') {
  109.             readch();  x -= term();
  110.         } else break;
  111.     return x;
  112. }
  113.  
  114. int    calc( char **str )
  115. {
  116.     char    *p;
  117.     char    buf[256];
  118.  
  119.     int    n = 0;
  120.     int    x;
  121.     int    dummy = 0;
  122.  
  123.     p=*str;
  124.     if(*p!='(')    errMsg(MSG_syntax,expMsg);
  125.     p++;
  126.     for(;;){
  127.         p=skipSpace(p);
  128.         if(*p=='\0')    errMsg(MSG_syntax,expMsg);
  129.         if(*p=='(')    dummy++;
  130.         if(*p==')')    dummy--;
  131.         if(dummy<0) {
  132.             buf[n]='\0';
  133.             break;
  134.         }
  135.         buf[n]=*p;
  136.         p++;
  137.         n++;
  138.         if(n>255)    errMsg(MSG_syntax,expMsg);
  139.     }
  140.     p++;
  141.     *str=p;
  142.  
  143.     expBuf=&buf[0];
  144.  
  145.     readch();  x = expression();
  146.     if (ch != '\0') errMsg(MSG_syntax,expMsg);
  147.     return(x);
  148. }
  149.  
  150. /* その他 */
  151.  
  152. int    comSearch( char *c[],char *s )    /* コマンド検索 */
  153. {
  154.     int    n = 0;
  155.     int    ret = ERR;
  156.  
  157.     while( c[n][0] != '\0' ) {
  158.         if( strncmp( s,c[n],strlen( c[n] )) == 0 ) {
  159.             ret = n;
  160.             break;
  161.         }
  162.         n++;
  163.     }
  164.     return( ret );
  165. }
  166.  
  167. char    *skipSpace( char *s )    /* スペースを飛ばす */
  168. {
  169.     while( *s == ' ' )
  170.         s++;
  171.     return( s );
  172. }
  173.  
  174. void    skipLine( char **s )    /* 空行を飛ばす */
  175.     /* [EOF]だった場合はNULLを指しているポインターを返す */
  176. {
  177.     char    *p;
  178.     char    *q;
  179.  
  180.     p = *s;
  181.     p = skipSpace( p );
  182.  
  183.     for(;;) {
  184.         q = p;
  185.         if( *p == '\0' ) {
  186.             if(( p = Pre_fgets()) == NULL ) {
  187.                 p = q;
  188.                 break;    /* [EOF] */
  189.             }
  190.             p = skipSpace( p );
  191.         }
  192.         if( *p != '\0' )    break;
  193.     }
  194.     *s = p;
  195. }
  196.  
  197. int    power( int x ,int y ) /* int型の累乗 */
  198. {
  199.     int    i;
  200.     long    ret = x;
  201.  
  202.     if ( y == 0 )
  203.         return(1);
  204.     for( i=1;i<y;i++ )
  205.         ret = ret*x;
  206.     return( (int)ret );
  207. }
  208.  
  209. double    sine( double xx ) /* サイン(度からラジアンに変換)*/
  210. {
  211.     double    x = ((xx)/180*3.14159265358979323846);    /* deg>rad */
  212.     return( sin(x) );
  213. }
  214.  
  215. int    instr( const char *s ,int c ) /* 1文字検索 */
  216. {
  217.     int    i;
  218.     int    l;
  219.     int    ret = ERR;
  220.  
  221.     l = strlen( s );
  222.     for( i=0;i<l;i++ ) {
  223.         if( *(s+i) == c ) {
  224.             ret = i;
  225.             break;
  226.         }
  227.     }
  228.     return( ret );
  229. }
  230.  
  231. int    strToInt( char **p ,int min ,int max ,int d ,char *m2 )
  232.     /* アスキー文字からint型への変換 */
  233. {
  234.     char    *msg = "(param)";
  235.     char    *s;
  236.     int    pm = 1;    /* plus or minas */
  237.     int    flags = NO;
  238.     int    dummy;
  239.     long    int    dat = 0;
  240.     long    int    da2 = 0;
  241.  
  242.     if ( m2 != NULL )    msg = m2;
  243.     s = skipSpace( *p );
  244.  
  245.     switch( *s ) {
  246.         case '+':
  247.             s++;
  248.             break;
  249.         case '-':
  250.             s++;
  251.             pm = -1;
  252.             break;
  253.         case '?':
  254.             s++;
  255.             dat = (long)calc( &s );
  256.             goto    Point;
  257.     }
  258.     
  259.     for(;;) {
  260.         dummy = toupper( *s );
  261.         dummy -= '0';
  262.         if( dummy < 0 || dummy > 9 )    break;
  263.         dat = dummy + dat*10;
  264.         da2 = dat*pm;
  265.         if( da2 > INT_MAX || da2 < INT_MIN )
  266.             errMsg( MSG_illfnc ,msg );
  267.         flags = YES;
  268.         s++;
  269.     }
  270.     dat = dat*pm;
  271.     if( flags == NO )
  272.         dat = (long)d;
  273. Point:
  274.     if( dat < min || dat > max )
  275.         errMsg( MSG_illfnc ,msg );
  276.     *p = s;
  277.     return( (int)dat );
  278. }
  279.